<template>
  <div class="pushList">
    <el-row style="text-align:right;margin: 10px 0">
      <el-button type="primary" @click="add">新增营销活动</el-button>
    </el-row>
    <el-row>
      <el-table :data="noticeData" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="200" align="center" :index="indexSort"></el-table-column>
        <el-table-column prop="title" label="标题" align="center"></el-table-column>
        <el-table-column prop="imageUrl" label="背景图片" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.imageUrl" alt width="50" />
          </template>
        </el-table-column>
        <el-table-column prop="linkUrl" label="跳转路径" align="center"></el-table-column>
        <el-table-column sortable prop="status" label="状态" align="center">
          <template slot-scope="props">
            <span v-if="props.row.status == 0" style="color:red">已下架</span>
            <span v-if="props.row.status == 1" style="color: rgb(103,194,58)">已上架</span>
          </template>
        </el-table-column>
        <el-table-column sortable prop="type" label="类型" align="center"></el-table-column>
        <el-table-column prop="updateDate" label="更新时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <span
              class="el-tag el-tag--warning el-tag--small"
              style="cursor: pointer;"
              @click="modify(scope.row)"
              size="mini"
            >修改</span>
            <span
              class="el-tag el-tag--danger el-tag--small"
              style="cursor: pointer;"
              @click="del(scope.row)"
              size="mini"
            >删除</span>
            <span
              class="el-tag el-tag--primary el-tag--small"
              style="cursor: pointer;"
              @click="upShelf(scope.row)"
              size="mini"
            >上架</span>
            <span
              class="el-tag el-tag--primary el-tag--small"
              style="cursor: pointer;"
              @click="downShelf(scope.row)"
              size="mini"
            >下架</span>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="text-align: right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[15, 50, 100, 200]"
        :page-size="pageSize"
        layout="->, sizes, prev, pager, next, jumper, total"
        :total="totalElements"
      ></el-pagination>
    </el-row>
    <addActivity :off.sync="avtivityDialog" :rowData="rowdata" :title="title"></addActivity>
  </div>
</template>

<script>
import {
     getActivityList,
     upOrDownActivity,
     delActivity,
    } from "@/api/supermarket/activity.js";
import * as util from "@/util/util.js";
export default {
  name: "activity",
  components: {
    addActivity: () => import("@/views/goodsSupermarket/marketing/addActivity")
  },
  data() {
    return {
      noticeData: [],
      pageSize: 15,
      currentPage: 1,
      totalElements: 0,
      totalPages: 1,
      avtivityDialog: false,
      rowdata: {},
      title: "",
      modifyImg: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    add() {
      this.title = "新增营销活动";
      this.avtivityDialog = true;
    },
    modify(row) {
      console.log("row", row);
      this.rowdata = row;
      this.title = "修改营销活动";
      this.avtivityDialog = true;
    },
    upShelf(row) {
        upOrDownActivity({
            carouselId: row.carouselId,
            status: 1
        }).then(
            res => {
                this.$notify.success("上架成功");
                this.getList();
            },
            err => {}
        );
    },
    downShelf(row) {
         upOrDownActivity({
            carouselId: row.carouselId,
            status: 0
        }).then(
            res => {
                this.$notify.success("下架成功");
                this.getList();
            },
            err => {}
        );
    },
    del(row) {
        delActivity({
            id: row.carouselId,
        }).then(
            res => {
                this.$notify.success("删除成功");
                this.getList();
            },
            err => {}
        );
    },
    getList() {
      getActivityList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        type: 1
      }).then(
        res => {
          console.log("res营销活动接口", res);
          this.noticeData = res.data.list;
          this.noticeData.forEach(e => {
            e.updateDate = util.timestampToTime(e.updateDate);
            if (e.type == 1) {
              e.type = "店铺";
            }
          });
          this.totalElements = res.data.total;
        },
        err => {}
      );
    },
    //序号
    indexSort(index) {
      if (this.currentPage > 1) {
        index = (this.currentPage - 1) * this.pageSize + index + 1;
      } else {
        index = index + 1;
      }
      return index;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

<style lang="less" props>
.pushList {
}
</style>